/**
 * 
 */
package org.rvk.vms.web.controller.secure;

import javax.servlet.http.HttpServletRequest;

import org.rvk.vms.api.volunteers.IVolunteersService;
import org.rvk.vms.dal.entities.Volunteer;
import org.rvk.vms.dal.model.VolunteersSummary;
import org.rvk.vms.dal.model.search.SearchInput;
import org.rvk.vms.dal.model.search.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ashish Kumar Sinha <aksinha.mail@gmail.com>
 *
 */
@Controller
@RequestMapping(value="/secure/volunteers")
public class VolunteerController {

    @Autowired
    private IVolunteersService volunteersService;

	@RequestMapping(value="/all")
	public ModelAndView all(HttpServletRequest request){
		request.setAttribute("page", "VOLUNTEERS");
		request.setAttribute("subpage", "ALL");

        VolunteersSummary volunteersSummary = volunteersService.getVolunteersSummary();
		
		ModelAndView modelAndView = new ModelAndView("secure/volunteer/all") ;
        modelAndView.addObject("volunteersSummary", volunteersSummary);

		return modelAndView;
	}
	
	@RequestMapping(value="/search")
	public ModelAndView search(HttpServletRequest request, @RequestParam(value = "key", required = false) String key){
		request.setAttribute("page", "VOLUNTEERS");
		request.setAttribute("subpage", "SEARCH");

        ModelAndView modelAndView = new ModelAndView("secure/volunteer/search");
        boolean hasUserSearched = false;

        if(key == null || key.trim().isEmpty()) {
            modelAndView.addObject("hasUserSearched", hasUserSearched);
            return modelAndView;
        }
        hasUserSearched = true;

        /*
            right now proceed with default pagesize and first page.
            Later on, these options can be taken through a parameter.
         */
        SearchInput searchInputKey = new SearchInput(key);
        SearchResult<Volunteer> searchResult = volunteersService.search(searchInputKey);

        modelAndView.addObject("hasUserSearched", hasUserSearched);
        modelAndView.addObject("searchResult", searchResult.getResult());
		return modelAndView;
	}
	
	@RequestMapping(value="/new")
	public ModelAndView newVolunteer(HttpServletRequest request){
		request.setAttribute("page", "VOLUNTEERS");
		request.setAttribute("subpage", "NEW");
		
		ModelAndView modelAndView = new ModelAndView("secure/volunteer/new") ;
		return modelAndView;
	}
	
	@RequestMapping(value="/save")
	public ModelAndView save(HttpServletRequest request){
		request.setAttribute("page", "VOLUNTEERS");
		request.setAttribute("subpage", "ALL");
		//TODO - Add successful or failure message
		ModelAndView modelAndView = new ModelAndView("secure/volunteer/all") ;
		return modelAndView;
	}
}
